﻿Imports TBCC.UserManage

Public Class Program

    Private Shared frmMain As New MainForm

    Public Shared Sub Main()


        Application.EnableVisualStyles()
        Application.DoEvents()



        'Dim splash As New SplashForm()
        'splash.ShowDialog()
        'User.Instance.Login("a", "a")
        'Dim frmLogin As New LoginForm
        'If frmLogin.ShowDialog = DialogResult.Cancel Then
        '    Exit Sub
        'End If


        'User.Instance.Login("admin", "admin")

        'Log.log(tbccLogOperateType.Login) '''


        ' AddHandler frmMain.FormClosed, AddressOf app_exit
        CheckUserRight(frmMain, False)
        Select Case frmMain.readConfig("SystemType")
            Case 0
                frmMain.TimerCarUploadDB.Start()
                frmMain.TimerFDAP_DATA.Start()
                frmMain.TimerFAP_Rel.Start()
            Case Else
        End Select
        ' Application.Run(frmMain)
        ' frmMain.Show()
        'frmMain.Hide()
    End Sub

    Public Shared Sub HideMainForm()
        frmMain.Hide()
    End Sub

    Public Shared Sub ShowMainForm()
        frmMain.Show()
        frmMain.WindowState = FormWindowState.Maximized
    End Sub

    Public Shared Sub RegShowMethod(ByVal method As ShowSelect)
        frmMain.RegShowMethod(method)
    End Sub

    Public Shared Sub SwitchUser()
        frmMain.SwitchUser()
    End Sub

    Public Shared Function IsHasMobile() As Boolean
        Dim il As IList(Of ProjectInfo.CarProjectInfo)
        il = CarProjectService.GetAllCarProjectInfo()
        If il IsNot Nothing Then
            If il.Count > 0 Then
                Return True
            End If
        End If
        Return False
    End Function

    Public Shared Function g_offline() As Boolean
        Return frmMain.IsOffline
    End Function

    ''' <summary>
    ''' 程序退出
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Public Shared Sub app_exit(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs)
        'Log.log(tbccLogOperateType.Login, "注销") '''
    End Sub

    'online: 表示是否已经连接
    Public Shared Sub CheckUserRight(ByVal frm As MainForm, ByVal IsOnline As Boolean)

        Dim gp As System.Security.Principal.GenericPrincipal
        gp = My.User.CurrentPrincipal

        Dim IsPrjSel As Boolean = False  '工程是否选择
        ' Dim l As IList(Of ProjectInfo.CarProjectInfo)
        '_isModified = False
        Dim prjList As IList(Of ProjectInfo.CarProjectInfo) = CarProjectService.GetAllCarProjectInfo
        Dim count = prjList.Count
        If count > 0 Then
            IsPrjSel = True
        End If

        For Each item As Object In frm.MenuStrip1.Items
            If TypeOf item Is ToolStripMenuItem Then
                Dim tsi As ToolStripMenuItem = CType(item, ToolStripMenuItem)
                setTsmiVisable(tsi, False)
                tsi.Visible = False
            End If
        Next


        If count <= 0 Then
            '[帮助]
            frm.mnuhelp.Visible = True
            '关于
            frm.mnuAbout.Visible = True

            frm.tsbMain.Visible = False
            frm.tsbHistCurve.Visible = False
            frm.ToolStripSeparator1.Visible = False
            frm.ToolStripSeparator23.Visible = False
            frm.ToolStripSeparator24.Visible = False
            frm.HisDatastat.Visible = False

            If gp.IsInRole(RoleClass.GetRolename(RoleType.HighLevel)) Then
                '[工程管理]
                frm.MenuItemProjectConfig.Visible = True
                '终端导入
                frm.mnuprjload.Visible = True
                '终端管理
                frm.MenuDevadminItem.Visible = True

                '[通讯]
                frm.MenuItemComm.Visible = True
                '终端扫描
                frm.mnuScanTerminal.Visible = True

            End If
            Exit Sub
        End If


        '所有用户都有的菜单


        frm.tsbMain.Visible = True
        frm.tsbHistCurve.Visible = True
        frm.ToolStripSeparator1.Visible = True
        frm.ToolStripSeparator23.Visible = True
        frm.ToolStripSeparator24.Visible = True
        frm.HisDatastat.Visible = True

        '[帮助]
        frm.mnuhelp.Visible = True
        '关于
        frm.mnuAbout.Visible = True



        '[冷链文档管理]
        frm.MenuItemColdAdmin.Visible = True
        '历史数据        
        frm.tsmiHisDataQuery.Visible = True

        '数据统计菜单
        frm.mnuHistDataDoc.Visible = True

        '[数据分析]
        frm.MenuItemData.Visible = True
        '实时数据
        frm.mnuRealtimeData.Visible = True

        '高级用户
        If gp.IsInRole(RoleClass.GetRolename(RoleType.HighLevel)) Then
            '[工程管理]
            frm.MenuItemProjectConfig.Visible = True

            frm.FDAPMenuItem.Visible = True

            frm.CarProSetItem.Visible = True
            frm.CarPortSetMenuItem.Visible = True
            frm.CarDataSetMenuItem.Visible = True

            '终端导出
            frm.mnuprjsave.Visible = True
            '终端导入
            frm.mnuprjload.Visible = True
            '终端管理
            frm.MenuDevadminItem.Visible = True
            '通讯管理
            frm.MenuWebAddItem.Visible = True

            '[通讯]
            frm.MenuItemComm.Visible = True
            '终端扫描
            frm.mnuScanTerminal.Visible = True

        End If

        '中级用户
        If gp.IsInRole(RoleClass.GetRolename(RoleType.MiddleLevel)) Then
            '[工程管理]
            frm.MenuItemProjectConfig.Visible = True
            '终端导出
            frm.mnuprjsave.Visible = True
            '终端导入
            frm.mnuprjload.Visible = True
            '终端管理
            frm.MenuDevadminItem.Visible = True

            Select Case MainForm.readConfig("SystemType")
                Case 0
                    '通讯管理
                    frm.MenuWebAddItem.Visible = True
                    frm.FDAPMenuItem.Visible = True
                    frm.CarProSetItem.Visible = True
                    frm.CarPortSetMenuItem.Visible = True
                    frm.CarDataSetMenuItem.Visible = True

                Case Else
                    '通讯管理
                    frm.MenuWebAddItem.Visible = False
                    frm.FDAPMenuItem.Visible = False
                    frm.CarProSetItem.Visible = False
                    frm.CarPortSetMenuItem.Visible = False
                    frm.CarDataSetMenuItem.Visible = False
            End Select


            
        End If

    End Sub

    Public Shared Sub setTsmiVisable(ByVal tsmiItem As ToolStripMenuItem, ByVal status As Boolean)
        For Each item As Object In tsmiItem.DropDownItems
            If TypeOf item Is ToolStripMenuItem Then
                If item.DropDownItems.Count > 0 Then
                    setTsmiVisable(item, status)
                End If
                item.Visible = status
            End If
            If TypeOf item Is ToolStripSeparator Then
                item.Visible = status
            End If
        Next
    End Sub

#Region "GPS"

    ''' <summary>
    ''' 根据GPS纬度值和方向获取显示的纬度信息
    ''' </summary>
    ''' <param name="Latitude">纬度数据</param>
    ''' <param name="Latitude_Dir">纬度方向</param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared Function Gps_GetFormatLatitude(ByVal Latitude As Single, ByVal Latitude_Dir As Integer) As String

        Dim str As String

        If Math.Abs(Latitude - INVALID_DATA) <= 0.001 Then '无效数据
            Return INVALID_FORMAT
        Else
            str = Math.Floor(Latitude / 100) & "°" & (Latitude - Math.Floor(Latitude / 100) * 100).ToString("0.00") & "′ "  '纬度
        End If

        str &= LATITUDEDIR(IIf((Latitude_Dir And GpsStateMask(0)) = 0, 0, 1))

        Return str

    End Function

    ''' <summary>
    ''' 根据GPS经度值和方向获取显示的经度信息
    ''' </summary>
    ''' <param name="Longitude">经度数据</param>
    ''' <param name="Longitude_Dir">经度方向</param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared Function Gps_GetFormatLongitude(ByVal Longitude As Single, ByVal Longitude_Dir As Integer) As String

        Dim str As String

        If Math.Abs(Longitude - INVALID_DATA) <= 0.001 Then '无效数据
            Return INVALID_FORMAT
        Else
            str = Math.Floor(Longitude / 100) & "°" & (Longitude - Math.Floor(Longitude / 100) * 100).ToString("0.00") & "′ "  '经度
        End If

        str &= LONGITUDEDIR(IIf((Longitude_Dir And GpsStateMask(1)) = 0, 0, 1))

        Return str

    End Function

#End Region

    ''' <summary>
    ''' 根据AI值返回显示的AI信息
    ''' </summary>
    ''' <param name="AiVal">AI输入值</param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared Function ConvertFormatAI(ByVal AiVal As Single) As String

        If Math.Abs(AiVal - INVALID_DATA) <= 0.001 Then '无效数据
            Return INVALID_FORMAT
        End If

        Return AiVal.ToString("0.00")

    End Function


End Class

Public Delegate Sub ShowSelect()